//
//  AccountInfoViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/30.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "AccountInfoViewController.h"
#import "WithdrawViewController.h"
#import "HWZBAPIRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "UIImageView+Webcache.h"
#import "MBProgressHUD+Toast.h"
#import "Context.h"
#import "MBProgressHUD+Toast.h"
#import "NSString+Utils.h"

@interface AccountInfoViewController ()

@property (strong, nonatomic) UILabel *diamondCreditsLabel;      //钻石苹果积分
@property (strong, nonatomic) UILabel *goldCreditsLabel;         //金苹果积分
@property (strong, nonatomic) UILabel *silverCreditsLabel;       //银苹果积分
@property (strong, nonatomic) UILabel *delegateCreditsLabel;     //代理积分
@property (strong, nonatomic) UIButton *rechargeButton;
@property (strong, nonatomic) UIButton *withdrawButton;

@end

@implementation AccountInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"账户中心";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountInfoChangeNotifyAction:) name:kNotificationAccountInfoChange object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)accountInfoChangeNotifyAction:(NSNotification *)notification {
    //获取用户信息
    [self loadData];
}

#pragma mark - UIButton Action
//立即充值
- (void)rechargeButtonAction:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请直接联系客户充值" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

//立即提现
- (void)withdrawButtonAction:(id)sender {
    NSString *goldenGredits = self.goldCreditsLabel.text;
    if([NSString isBlankString:goldenGredits] || [goldenGredits integerValue] <= 0) {
        [MBProgressHUD showToast:@"您没有足够的积分可提现!" initWithView:self.view];
        return;
    } else {
        WithdrawViewController *withdrawViewController = [[WithdrawViewController alloc] init];
        [self.navigationController pushViewController:withdrawViewController animated:YES];
    }
}

#pragma mark - Private Method
- (void)createUI {
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 64.0f - TRANS_VALUE(98.0f))];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"bg_login"];
    bgImageView.userInteractionEnabled = YES;
    [self.view addSubview:bgImageView];
    
    UILabel *diamondTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(30.0f), SCREEN_WIDTH, TRANS_VALUE(20.0f))];
    diamondTitleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    diamondTitleLabel.textColor = I_COLOR_WHITE;
    diamondTitleLabel.textAlignment = NSTextAlignmentCenter;
    diamondTitleLabel.text = @"钻石苹果";
    [bgImageView addSubview:diamondTitleLabel];
    
    self.diamondCreditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(60.0f), SCREEN_WIDTH, TRANS_VALUE(30.0f))];
    self.diamondCreditsLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(18.0f)];
    self.diamondCreditsLabel.textColor = I_COLOR_WHITE;
    self.diamondCreditsLabel.textAlignment = NSTextAlignmentCenter;
    self.diamondCreditsLabel.text = @"2000";
    [bgImageView addSubview:self.diamondCreditsLabel];
    
    self.rechargeButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(90.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(100.0f), TRANS_VALUE(36.0f))];
    self.rechargeButton.clipsToBounds = YES;
    self.rechargeButton.layer.borderColor = I_COLOR_WHITE.CGColor;
    self.rechargeButton.layer.borderWidth = 1.0f;
    self.rechargeButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.rechargeButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.rechargeButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.rechargeButton setTitle:@"立即充值" forState:UIControlStateNormal];
    [self.rechargeButton setTitle:@"立即充值" forState:UIControlStateSelected];
    [bgImageView addSubview:self.rechargeButton];
    
    
    UILabel *goldTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(160.0f), SCREEN_WIDTH, TRANS_VALUE(20.0f))];
    goldTitleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    goldTitleLabel.textColor = I_COLOR_WHITE;
    goldTitleLabel.textAlignment = NSTextAlignmentCenter;
    goldTitleLabel.text = @"金苹果";
    [bgImageView addSubview:goldTitleLabel];
    
    self.goldCreditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(180.0f), SCREEN_WIDTH, TRANS_VALUE(30.0f))];
    self.goldCreditsLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(18.0f)];
    self.goldCreditsLabel.textColor = I_COLOR_WHITE;
    self.goldCreditsLabel.textAlignment = NSTextAlignmentCenter;
    self.goldCreditsLabel.text = @"0";
    [bgImageView addSubview:self.goldCreditsLabel];
    
    self.withdrawButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(210.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(100.0f), TRANS_VALUE(36.0f))];
    self.withdrawButton.clipsToBounds = YES;
    self.withdrawButton.layer.borderColor = I_COLOR_WHITE.CGColor;
    self.withdrawButton.layer.borderWidth = 1.0f;
    self.withdrawButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.withdrawButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.withdrawButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.withdrawButton setTitle:@"立即提现" forState:UIControlStateNormal];
    [self.withdrawButton setTitle:@"立即提现" forState:UIControlStateSelected];
    [bgImageView addSubview:self.withdrawButton];
    
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(260.0f), SCREEN_WIDTH, 0.5f)];
    dividerView.backgroundColor = I_COLOR_WHITE;
    [bgImageView addSubview:dividerView];
    
    self.silverCreditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(290.0f), SCREEN_WIDTH / 2, TRANS_VALUE(30.0f))];
    self.silverCreditsLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(18.0f)];
    self.silverCreditsLabel.textColor = I_COLOR_WHITE;
    self.silverCreditsLabel.textAlignment = NSTextAlignmentCenter;
    self.silverCreditsLabel.text = @"2000";
    [bgImageView addSubview:self.silverCreditsLabel];
    
    UILabel *silverTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(320.0f), SCREEN_WIDTH / 2, TRANS_VALUE(20.0f))];
    silverTitleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    silverTitleLabel.textColor = I_COLOR_WHITE;
    silverTitleLabel.textAlignment = NSTextAlignmentCenter;
    silverTitleLabel.text = @"银苹果";
    [bgImageView addSubview:silverTitleLabel];
    
    self.delegateCreditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, TRANS_VALUE(290.0f), SCREEN_WIDTH / 2, TRANS_VALUE(30.0f))];
    self.delegateCreditsLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(18.0f)];
    self.delegateCreditsLabel.textColor = I_COLOR_WHITE;
    self.delegateCreditsLabel.textAlignment = NSTextAlignmentCenter;
    self.delegateCreditsLabel.text = @"0";
    [bgImageView addSubview:self.delegateCreditsLabel];
    
    UILabel *delegateTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, TRANS_VALUE(320.0f), SCREEN_WIDTH / 2, TRANS_VALUE(20.0f))];
    delegateTitleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    delegateTitleLabel.textColor = I_COLOR_WHITE;
    delegateTitleLabel.textAlignment = NSTextAlignmentCenter;
    delegateTitleLabel.text = @"代理";
    [bgImageView addSubview:delegateTitleLabel];
    
    
    [self.rechargeButton addTarget:self action:@selector(rechargeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.withdrawButton addTarget:self action:@selector(withdrawButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    NSString *loginId = [Context sharedInstance].loginID;
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:loginId forKey:@"loginid"];
    [SVProgressHUD showWithStatus:@"正在获取账户信息..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getAccountInfoWithParams:params success:^(AccountInfo *accountInfo) {
        [SVProgressHUD dismiss];
        if(accountInfo) {
            [self showAccountInfo:accountInfo];
        } else {
            [self showAccountInfo:nil];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [self showAccountInfo:nil];
    }];
}

- (void)showAccountInfo:(AccountInfo *)accountInfo {
    if(accountInfo) {
        NSString *diamondGredits = accountInfo.diamondAccount != nil ? accountInfo.diamondAccount : @"0";
        self.diamondCreditsLabel.text = diamondGredits;
        NSString *goldGredits = accountInfo.goldenAccount != nil ? accountInfo.goldenAccount : @"0";
        self.goldCreditsLabel.text = goldGredits;
        NSString *silverGredits = accountInfo.silverAccount != nil ? accountInfo.silverAccount : @"0";
        self.silverCreditsLabel.text = silverGredits;
        NSString *delegateGredits = accountInfo.agentAccount != nil ? accountInfo.agentAccount : @"0";
        self.delegateCreditsLabel.text = delegateGredits;
    } else {
        self.diamondCreditsLabel.text = @"0";
        self.goldCreditsLabel.text = @"0";
        self.silverCreditsLabel.text = @"0";
        self.delegateCreditsLabel.text = @"0";
    }
}

@end
